package handler

import (
	"context"
	"gitee.com/fangy1111/shop/userop"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"userop_srv/global"
	"userop_srv/model"
)

type UserOpServer struct {
	userop.UnimplementedAddressServer
	userop.UnimplementedMessageServer
	userop.UnimplementedUserFavServer
}

// 获取地址列表
func (u UserOpServer) GetAddressList(ctx context.Context, request *userop.AddressRequest) (*userop.AddressListResponse, error) {
	var address []*model.Address
	tx := global.DB.Model(&model.Address{}).Where(model.Address{
		UserId: request.UserId,
	}).Find(&address)

	if tx.Error != nil {
		return nil, status.Errorf(codes.Internal, "获取列表失败")
	}

	var count int64
	global.DB.Model(&model.Address{}).Where(model.Address{
		UserId: request.UserId,
	}).Count(&count)
	var addressResponse []*userop.AddressResponse

	for _, v := range address {
		addressResponse = append(addressResponse, &userop.AddressResponse{
			Id:           int32(v.ID),
			UserId:       v.UserId,
			Province:     v.Province,
			City:         v.City,
			District:     v.District,
			Address:      v.Address,
			SignerName:   v.SignerName,
			SignerMobile: v.SignerMobile,
		})
	}

	return &userop.AddressListResponse{
		Total: int32(count),
		Data:  addressResponse,
	}, nil
}

// 添加地址
func (u UserOpServer) CreateAddress(ctx context.Context, request *userop.AddressRequest) (*userop.AddressResponse, error) {
	address := model.Address{
		UserId:       request.UserId,
		Province:     request.Province,
		City:         request.City,
		District:     request.District,
		Address:      request.Address,
		SignerName:   request.SignerName,
		SignerMobile: request.SignerMobile,
	}

	tx := global.DB.Model(&model.Address{}).Create(&address)
	if tx.Error != nil {
		return nil, status.Errorf(codes.Internal, "添加地址失败")
	}
	return &userop.AddressResponse{
		Id:           int32(address.ID),
		UserId:       address.UserId,
		Province:     address.Province,
		City:         address.City,
		District:     address.District,
		Address:      address.Address,
		SignerName:   address.SignerName,
		SignerMobile: address.SignerMobile,
	}, nil
}

// 删除地址
func (u UserOpServer) DeleteAddress(ctx context.Context, request *userop.AddressRequest) (*userop.AddressEmpty, error) {
	var address model.Address

	tx := global.DB.Model(&model.Address{}).Where("id = ? and user_id = ?", request.Id, request.UserId).Delete(&address)

	if tx.Error != nil {
		return nil, status.Errorf(codes.Internal, "添加地址失败")
	}

	return &userop.AddressEmpty{}, nil
}

// 更新地址
func (u UserOpServer) UpdateAddress(ctx context.Context, request *userop.AddressRequest) (*userop.AddressEmpty, error) {
	var address model.Address

	address.Province = request.Province
	address.City = request.City
	address.District = request.District
	address.Address = request.Address
	address.SignerName = request.SignerName
	address.SignerMobile = request.SignerMobile

	tx := global.DB.Model(&model.Address{}).Where("id = ? and user_id = ?", request.Id, request.UserId).Updates(&address)

	if tx.Error != nil {
		return nil, status.Errorf(codes.Internal, "修改地址失败")
	}

	return &userop.AddressEmpty{}, nil
}
